package com.card323.crpg.battle;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;

import com.card323.crpg.Hero;
import com.card323.crpg.battle.buff.Buff;
import com.card323.crpg.exception.CRPGWrongIndexException;
import com.card323.crpg.skill.Skill;
import com.card323.crpg.skill.battle.BattleEvent;
import com.card323.crpg.skill.battle.BattleEventHandler;

public class HeroBattleInfo {

	private boolean _is_in_battle;
	private BattleEngine _battle;
	ArrayList<Buff> _buffs = new ArrayList<Buff>();
	Hero _hero;
	private ActionPoint _ap = new ActionPoint();

	public HeroBattleInfo(Hero hero) {
		_hero = hero;
	}

	public void remove_buff(Buff buff) {
		_buffs.remove(buff);
	}

	public void add_buff(Buff b) {
		_buffs.add(b);
	}

	public boolean can_skill(Skill skill) {
		return get_ap().is_bigger_than(skill.get_active_ap_cost());
	}

	public boolean can_attack() {
		return get_ap().is_bigger_than(_hero._attack_speed);

	}

	public void active_skill(String skill_cmd) throws CRPGWrongIndexException {
		Skill skill = _hero._skills.get(skill_cmd);
		if (skill == null) {
			_hero.send_message("技能" + skill_cmd + "未设置!", Color.RED);
			return;
		}

		if (!can_skill(skill)) {
			_hero.send_message(
					"行动点数不足 " + _hero.get_name() + "当前行动点数"
							+ get_ap().toString() + ",所需行动点数"
							+ skill.get_active_ap_cost(), Color.RED);
			return;
		}
		if (skill.is_need_target()) {
			_hero.send_message("请输入目标编号:", Color.BLUE);
			_hero.send_message(_battle.get_targets_list(_hero));
			try {
				int index = Integer.valueOf(_hero.get_message().trim());

				Hero target = _battle.get_target_hero(_hero, index);
				if (target == null) {
					_hero.send_message("目标" + index + "不存在", Color.RED);
					return;
				}
				ArrayList<Hero> targets = new ArrayList<Hero>();
				targets.add(target);
				skill.active(_hero, targets);
			} catch (NumberFormatException exc) {
				exc.printStackTrace();
				return;
			}

		} else {
			skill.active(_hero, null);
		}
	}

	public void send_message_to_all(String msg) {

		for (Hero h : _battle._team1._heros) {
			h.send_message(msg);
		}
		for (Hero h : _battle._team2._heros) {
			h.send_message(msg);
		}

	}

	public void send_message_to_allies(String msg, Color c) {

		for (Hero h : this._battle.get_allies_team(_hero)._heros) {
			h.send_message(msg, c);
		}

	}

	public void send_message_to_enemy(String msg, Color c) {

		for (Hero h : this._battle.get_enemy_team(_hero)._heros) {
			h.send_message(msg, c);
		}

	}

	public void buff_update() {
		for (Iterator<Buff> iter = _buffs.iterator(); iter.hasNext();) {
			Buff b = iter.next();
			b._time -= 1;
			if (b._time <= 0)
				iter.remove();
		}

	}

	public int get_attack_dmg() {
		Random r = new Random();
		int dmg = _hero._base_attack;
		if (_hero._random_attack > 0 && _hero._random_attack_times > 0)
			for (int i = 0; i < _hero._random_attack_times; i++) {

				dmg += r.nextInt(_hero._random_attack) + 1;
			}
		dmg += get_add_attack_dmg();
		return dmg;
	}

	private int get_add_attack_dmg() {
		int add = 0;
		for (Buff b : _buffs) {
			add += b._dmg_fix;
		}
		return add;
	}

	public void raise_event(BattleEvent battle_event) {
		for (Buff b : _buffs) {
			for (BattleEventHandler handler : b._battle_event_handlers) {
				if (b._trigger == battle_event._timing) {
					handler.handle_battle_event(b, battle_event);
				}
			}
		}

	}

	public ActionPoint get_ap() {
		return _ap;
	}

	public void battle_end() {
		_is_in_battle = false;
		_ap.clear();
		_battle = null;
		_buffs.clear();

	}

	public void battle_start(BattleEngine battleEngine) {
		_battle = battleEngine;
		_is_in_battle = true;

	}

	public void send_attack_cmd() {
		_battle.attack(_hero);

	}

	public boolean is_in_battle() {

		return _is_in_battle;
	}
}
